#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:taotieren


"""
    rpi.gpio 需要 root 权限，板上运行需要使用
    sudo python motor_control.py
    控制直流电机正反转
"""

""" python3 中请安装 tkintertoy 来进行程序调试和验证 """
from tkinter import *

""" 非树莓派环境下可以用 rpi.gpio_def 代替 rpi.gpio 来进行程序调试和验证 """
import RPi.GPIO as GPIO
import time

enable_pin = 18
in1_pin = 23
in2_pin = 24
GPIO.setmode(GPIO.BCM)
GPIO.setup(enable_pin, GPIO.OUT)
GPIO.setup(in1_pin, GPIO.OUT)
GPIO.setup(in2_pin, GPIO.OUT)

pwm = GPIO.PWM(enable_pin, 500)
pwm.start(0)


def clockwise():
    GPIO.output(in1_pin, True)
    GPIO.output(in2_pin, False)


def counter_clockwise():
    GPIO.output(in1_pin, False)
    GPIO.output(in2_pin, True)


while True:
    cmd = input("Command: f/r 0..9, E.g. f5:")
    direction = cmd[0]
    if direction == "f":
        clockwise()
    else:
        counter_clockwise()
    speed = int(cmd[1]) * 10
    pwm.ChangeDutyCycle(speed)
